package com.gimi.cloud.jimi.common.utils;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;

import java.io.*;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

/**
 * xml工具类
 * @author Administrator
 *
 */
public class XmlParserImpl {

	/** map集合 */
	private static Map<String, XmlParserImpl> maps = new Hashtable<String, XmlParserImpl>();
	/** 文件名 */
	public String fileName = null;
	/** 文件类 */
	public File files = null;
	/** 文档 */
	public Document document = null;
	/** 读者 */
	private SAXReader reader = null;
	
	private String encode = "utf-8";
	private String encodeType="utf-16";
	private static String defaultEncode = "utf-8";
	
	public XmlParserImpl(){
		
	}
	
	public XmlParserImpl(String encode){
		this.encode = encode;
	}

	/**
	 * 获取内流的
	 * 
	 * @param name
	 * @return
	 */
	public static XmlParserImpl getInstence(String name) {
		if (name == null) {
			return null;
		}
		XmlParserImpl _obj = maps.get(name);
		if (_obj == null) {
			_obj = new XmlParserImpl(defaultEncode);
			maps.put(name, _obj);
		}
		return _obj;
	}
	public static XmlParserImpl getInstence(String name, String encode) {
		if (name == null) {
			return null;
		}
		XmlParserImpl _obj = maps.get(name);
		if (_obj == null) {
			_obj = new XmlParserImpl(encode);
			maps.put(name, _obj);
		}
		return _obj;
	}
	/**
	 * 获得文档格式
	 * 
	 * @return
	 */
	public String getDoc() {
		document.setXMLEncoding(encode);
		return document.asXML();
	}

	/**
	 * 创建文档
	 */
	public void createDom() {
		document = DocumentHelper.createDocument();
	}

	/** 创建根文档 */
	public void createDomRoot(String rootElem) {
		document = DocumentHelper.createDocument();
		document.addElement(rootElem);
	}

	/** 设置文档格式 */
	public void setDoc(String doc) throws DocumentException {
		reader = new SAXReader();
		try {
			document = reader.read(new ByteArrayInputStream(doc
					.getBytes(encode)));
		} catch (UnsupportedEncodingException e) {
			 
		}
		document.setXMLEncoding(encode);
	}
	/** 设置文档格式  utf-16 */
	public void setDocs(String doc) throws DocumentException {
		reader = new SAXReader();
		try {
			document = reader.read(new ByteArrayInputStream(doc
					.getBytes(encodeType)));
		} catch (UnsupportedEncodingException e) {
			 
		}
		document.setXMLEncoding(encodeType);
	}

	/**
	 * 打开文件
	 * 
	 * @return
	 */
	public boolean openFile() {
		try {
			files = new File(fileName);
			if (!files.exists()) {
				files.createNewFile();
			}
			reader = new SAXReader();
			document = reader.read(files);
			document.setXMLEncoding(encode);
		} catch (Exception e) {
			 
			return false;
		}
		return true;
	}

	/**
	 * 获得节点的值
	 * 
	 * @param node
	 * @return
	 */

	@SuppressWarnings("rawtypes")
	public String getVal(String node) {
		List list = document.selectNodes(node);
		String _str = null;
		if (!list.isEmpty()) {
			Element element = (Element) list.get(0);
			_str = element.getText();
		}
		return _str;
	}
	
	@SuppressWarnings("rawtypes")
	public String getVal(Element parNode, String node) {
		List list = parNode.selectNodes(node);
		String _str = null;
		if (!list.isEmpty()) {
			Element element = (Element) list.get(0);
			_str = element.getText();
		}
		return _str;
	}

	/**
	 * 获得节点
	 * 
	 * @param node
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List getNodes(String node) {
		List list = document.selectNodes(node);
		return list;
	}

	/**
	 * 设置正文
	 * 
	 * @param node
	 * @param val
	 */
	public void setText(String node, String val) {
		Element noted = (Element) document.selectSingleNode(node);
		noted.setText(val);
	}

	/**
	 * 添加正文
	 * 
	 * @param node
	 * @param elem
	 * @return 
	 */
	public Element addElem(String node, String elem) {
		Element noted = (Element) document.selectSingleNode(node);
		return noted.addElement(elem);
	}

	/**
	 * 添加属性
	 * 
	 * @param node
	 * @param name
	 * @param value
	 */
	public void addAttribute(String node, String name, String value) {
		Element noted = (Element) document.selectSingleNode(node);
		noted.addAttribute(name, value);
	}

	/**
	 * 设置属性
	 * 
	 * @param node
	 * @param name
	 * @param value
	 */
	public void setAttribute(String node, String name, String value) {
		Element noted = (Element) document.selectSingleNode(node);
		noted.addAttribute(name, value);
	}

	/**
	 * 是否是退出节点
	 * 
	 * @param nodeName
	 * @return
	 */
	public boolean isExitsNode(String nodeName) {
		Element noted = (Element) document.selectSingleNode(nodeName);
		return noted != null;
	}

	/**
	 * 获取属性
	 * 
	 * @param node
	 * @param name
	 * @return
	 */
	public String getAttribute(String node, String name) {
		Element noted = (Element) document.selectSingleNode(node);
		if (noted == null) {
			return null;
		}
		return noted.attributeValue(name);
	}
	public String getAttribute(Element parNode, String node, String name) {
		Element noted = (Element) parNode.selectSingleNode(node);
		if (noted == null) {
			return null;
		}
		return noted.attributeValue(name);
	}

	/**
	 * 关闭文件
	 */
	public void closeFile() {
		files = null;
		document = null;
		reader = null;
	}

	/**
	 * 保存文件
	 * 
	 * @return
	 */
	public boolean saveFile() {

		try {
			if (files == null) {
				files = new File(fileName);
				if (!files.exists()) {
					files.createNewFile();
				}
			}
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setLineSeparator("\r\n");
			format.setEncoding(encode);
			OutputStreamWriter out = new OutputStreamWriter(
					new FileOutputStream(files), encode);
			out.write(document.asXML());
			out.flush();
			out.close();
		} catch (IOException e) {
			 
		}
		return true;
	}

	/**
	 * 另存为文件
	 * 
	 * @return
	 */
	public boolean saveAsFile() {

		try {
			files = new File(fileName);
			if (!files.exists()) {
				files.createNewFile();
			}
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setLineSeparator("\r\n");
			format.setEncoding(encode);
			OutputStreamWriter out = new OutputStreamWriter(
					new FileOutputStream(files), encode);
			out.write(document.asXML());
			out.flush();
			out.close();
		} catch (IOException e) {
			 
		}
		return true;
	}
	@SuppressWarnings("rawtypes")
	public List getNodes(Element element, String node) {
		List list = element.selectNodes(node);
		return list;
	}


	/**
	 * 测试方法
	 * 
	 * @param str
	 */
	public static void main(String str[]) {

		XmlParserImpl cqcrmxml = XmlParserImpl.getInstence("cqcrmxml");
		try {
			cqcrmxml.setDoc("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><name>ddvip</name></root>");
		    String ss=cqcrmxml.getVal("root/name");
			System.out.println(ss);
		} catch (DocumentException e) {
			 
		}
	}
}
